﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Diagnostics;
using System.Data;
namespace Test {
	class KontoGraph {
		static DAL dl=new DAL();

		public static void MainX() {

			//DAL dl=new DAL("pon-wpsq09-db","rf");
			//DataTable tab=dl.Query(@"select convert(char(16),ChangeTime,20)[day],address,count(bonus)[count]
			//from rfStateChangeBonusLog(nolock)where ChangeTime>getdate()-1
			//group by convert(char(16),ChangeTime,20),address order by 2");
			DataTable tab=dl.Query(@"select convert(char(10),day,20)[day],(max(rem)/6)-800[rem1],(min(rem)/6)-800[rem2] from konto(nolock)where day>getdate()-400 group by convert(char(10),day,20)order by 1");
			DateTime minD=DateTime.ParseExact(tab.Compute("min("+tab.Columns[0].Caption+")",null).ToString(),"yyyy-MM-dd",null);
			DateTime maxD=DateTime.ParseExact(tab.Compute("max("+tab.Columns[0].Caption+")",null).ToString(),"yyyy-MM-dd",null);
			//int maxI=(int)tab.Compute("max("+tab.Columns[2].Caption+")",null);
			int span=(int)(maxD-minD).TotalDays;
			Dictionary<string,List<Point>> lst=new Dictionary<string,List<Point>>();
			string fn="graphDet.png";
			int rowHeight=900;
			List<Point> points1=new List<Point>();
			List<Point> points2=new List<Point>();
			points1.Add(new Point(0,rowHeight));
			points2.Add(new Point(0,rowHeight));
			foreach(DataRow row in tab.Rows)
			{
				DateTime dat=DateTime.ParseExact(row[0].ToString(),"yyyy-MM-dd",null);
				int posX=(int)(dat-minD).TotalDays;
				int val1=(int)float.Parse(row[1].ToString());
				int val2=(int)float.Parse(row[2].ToString());

				if(points1.Count>1)
				{
					points1.Add(new Point(points1[points1.Count-1].X,rowHeight-val1));
					points2.Add(new Point(points2[points2.Count-1].X,rowHeight-val2));
				}

				points1.Add(new Point(posX*2,rowHeight-val1));
				points2.Add(new Point(posX*2,rowHeight-val2));


			}
			if(points1.Count>1)
			{
				points1.Add(new Point(points1[points1.Count-1].X,rowHeight));
				points2.Add(new Point(points2[points2.Count-1].X,rowHeight));
			}
			points1.Add(new Point(points1[points1.Count-1].X*2,rowHeight));
			points2.Add(new Point(points2[points2.Count-1].X*2,rowHeight));


			using(Bitmap bmp=new Bitmap(span*2+20,rowHeight))
			using(Graphics g=Graphics.FromImage(bmp))
			{

				g.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
				g.Clear(Color.White);
				g.DrawLine(Pens.Black,0,rowHeight-((5000/6)-800),span*2,rowHeight-((5000/6)-800));
				g.DrawLine(Pens.Black,0,rowHeight-((6000/6)-800),span*2,rowHeight-((6000/6)-800));
				g.DrawLine(Pens.Black,0,rowHeight-((7000/6)-800),span*2,rowHeight-((7000/6)-800));
				g.DrawLine(Pens.Black,0,rowHeight-((8000/6)-800),span*2,rowHeight-((8000/6)-800));
				g.DrawLine(Pens.Black,0,rowHeight-((9000/6)-800),span*2,rowHeight-((9000/6)-800));
		

				g.FillPolygon(new SolidBrush(Color.FromArgb(150,Color.Yellow)),points1.ToArray());
				g.DrawPolygon(Pens.Black,points1.ToArray());

				g.FillPolygon(new SolidBrush(Color.FromArgb(150,Color.GreenYellow)),points2.ToArray());
				g.DrawPolygon(Pens.Black,points2.ToArray());
		

				bmp.Save(fn,ImageFormat.Png);
			}
			Process.Start(fn);
		}

		static void Main3() {

			//DAL dl=new DAL("pon-wpsq09-db","rf");
			//DataTable tab=dl.Query(@"select convert(char(16),ChangeTime,20)[day],address,count(bonus)[count]
			//from rfStateChangeBonusLog(nolock)where ChangeTime>getdate()-1
			//group by convert(char(16),ChangeTime,20),address order by 2");

			DataTable tab=dl.Query(@"select convert(char(10),day,120)[day],'X',avg(rem)%5000/10[rem] from konto(nolock) group by convert(char(10),day,120)order by 1");
			DateTime minD=DateTime.ParseExact(tab.Compute("min("+tab.Columns[0].Caption+")",null).ToString(),"yyyy-MM-dd",null);
			DateTime maxD=DateTime.ParseExact(tab.Compute("max("+tab.Columns[0].Caption+")",null).ToString(),"yyyy-MM-dd",null);
			//int maxI=(int)tab.Compute("max("+tab.Columns[2].Caption+")",null);
			int span=(int)(maxD-minD).TotalDays;
			List<string> uniques=new List<string>();
			foreach(DataRow row in tab.Rows)
				if(!uniques.Contains(row[tab.Columns[1].Caption].ToString()))
					uniques.Add(row[tab.Columns[1].Caption].ToString());
			Dictionary<string,List<Point>> lst=new Dictionary<string,List<Point>>();
			string fn="graph.png";
			int rowHeight=515;
			using(Bitmap bmp=new Bitmap(span+20,10+(rowHeight*uniques.Count)*2))
			using(Graphics g=Graphics.FromImage(bmp))
			{
				g.Clear(Color.White);
				foreach(DataRow row in tab.Rows)
				{
					DateTime dat=DateTime.ParseExact(row[0].ToString(),"yyyy-MM-dd",null);
					int posX=(int)(dat-minD).TotalDays;
					int val=(int)float.Parse(row[2].ToString());
					g.DrawLine(new Pen(Color.FromArgb(130,Color.Red)),posX,rowHeight*2+uniques.IndexOf(row[tab.Columns[1].Caption].ToString())*rowHeight,posX,rowHeight*2+uniques.IndexOf(row[tab.Columns[1].Caption].ToString())*rowHeight-val);
				}
				bmp.Save(fn,ImageFormat.Png);
			}
			Process.Start(fn);
		}
		static void MainXs() {
			DataTable tab=dl.Query(@"select mth,av,mi,ma,remF,remL from(
select convert(char(6),day,12)mth,avg(rem)av,min(rem)mi,max(rem)ma,min(id)mii,max(id)mai
from konto(nolock)group by convert(char(6),day,12))t1
join (select id,rem remF from konto(nolock))t2 on t1.mii=t2.id
join (select id,rem remL from konto(nolock))t3 on t1.mai=t3.id
order by 1");
			string fn="graph.png";
			int barWidth=3;
			using(Bitmap bmp=new Bitmap(1500,650))
			using(Graphics g=Graphics.FromImage(bmp))
			{
				g.Clear(Color.White);
				for(int i=0;i<=580;i+=20)
					g.DrawLine(Pens.Silver,0,bmp.Height-25-i,bmp.Width,bmp.Height-25-i);
				for(int i=0;i<=600;i+=100)
					g.DrawLine(Pens.Black,0,bmp.Height-25-i,bmp.Width,bmp.Height-25-i);
				int left=5;
				foreach(DataRow row in tab.Rows)
				{
					int avg=(int)(decimal)row["av"];
					int mi=(int)(decimal)row["mi"];
					int ma=(int)(decimal)row["ma"];
					int remF=(int)(decimal)row["remF"];
					int remL=(int)(decimal)row["remL"];

					//g.DrawRectangle(Pens.Black,left+5,bmp.Height-100-(ma/150),0,(ma-mi)/150);

					g.DrawLine(Pens.Black,left+barWidth/2,bmp.Height-25-(ma/100),left+barWidth/2,bmp.Height-25-(mi/100));

					if(remL>remF)
					{
						g.FillRectangle(Brushes.DodgerBlue,left,bmp.Height-25-(remL/100),barWidth,(remL-remF)/100);
						g.DrawRectangle(Pens.Black,left,bmp.Height-25-(remL/100),barWidth,(remL-remF)/100);
					}
					else
					{
						g.FillRectangle(Brushes.Red,left,bmp.Height-25-(remF/100),barWidth,(remF-remL)/100);
						g.DrawRectangle(Pens.Black,left,bmp.Height-25-(remF/100),barWidth,(remF-remL)/100);
					}
					g.DrawLine(Pens.Black,left,bmp.Height-25-(avg/100),left+barWidth,bmp.Height-25-(avg/100));
					left+=barWidth;
				}
				bmp.Save(fn,ImageFormat.Png);
			}
			Process.Start(fn);
		}



	}
}
